﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Framework_Tool
{
    /// <summary>
    /// Interaction logic for LearnerMultipleChoice.xaml
    /// </summary>
    public partial class LearnerMultipleChoice : Page
    {
        MainWindow main;
        List<MultipleChoiceQuestion> q;
        TakeAssessment takeAsmnt;
        private int numberOfCorrect = 0;
        StackPanel sp = new StackPanel();

        public string resultString = "";

        public LearnerMultipleChoice(TakeAssessment takeAsmnt)
        {
            InitializeComponent();
            this.takeAsmnt = takeAsmnt;
            
            //Database.PopulateLrnerMc(this, takeAsmnt);
        }

        private void takeAssessmentBtn_Click_1(object sender, RoutedEventArgs e)
        {
            TakeAssessment taskeAsmntWindow = new TakeAssessment(main);
            taskeAsmntWindow.Content = null;
            mainContent.Children.Add(taskeAsmntWindow.mainContent);
        }

        private void assessmentProfileBtn_Click_1(object sender, RoutedEventArgs e)
        {
            LearnerAssessmentProfile lrnerAsmntProfileWindow = new LearnerAssessmentProfile();
            lrnerAsmntProfileWindow.Content = null;
            mainContent.Children.Add(lrnerAsmntProfileWindow.mainContent);
        }

        private void searchAssessmentBtn_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void mainContent_Loaded_1(object sender, RoutedEventArgs e)
        {
            List<String> mcQuestions = Database.PopulateMC(this);

            q = Database.PopulateLrnerMc(this, takeAsmnt);

            List<String> allQus = new List<string>();

            int questionNo = 1;
            StackPanel sub;
            foreach (MultipleChoiceQuestion mcQu in q)
            {
                sub = new StackPanel();
                TextBlock dynamicTxtB = new TextBlock();
                dynamicTxtB.Name = "mcQu" + questionNo;
                dynamicTxtB.FontSize = 16;
                dynamicTxtB.FontWeight = FontWeights.Bold;
                dynamicTxtB.Width = 585;
                dynamicTxtB.TextWrapping = TextWrapping.Wrap;
                dynamicTxtB.Background = Brushes.Transparent;
                dynamicTxtB.Foreground = Brushes.White;
                dynamicTxtB.Text = questionNo +  ". " + mcQu.question;
                sub.Children.Add(dynamicTxtB);            
                questionNo++;
                dynamicTxtB.Margin = new Thickness (0, 15, 0, 0);
                numberOfCorrect += mcQu.CorrectAnswers.Count;
                mcQu.allAnswers.Shuffle();
                foreach (String s in mcQu.allAnswers)
                {
                    CheckBox c = new CheckBox();
                    c.Foreground = Brushes.White;
                    c.FontSize = 14;
                    c.Content = s;
                    sub.Children.Add(c);
                }      
                sp.Children.Add(sub);
            }
                sp.Orientation = Orientation.Vertical;
                learnerMcAsmntGrid.Children.Add(sp);
        }

        private void submitMc_Click_1(object sender, RoutedEventArgs e)
        {
            int countRight = 0;
            int countWrong = 0;
            double percent = 0;
            foreach (object o in sp.Children)
            {
                if (o is StackPanel)
                {
                    StackPanel temp = (StackPanel)o;
                    foreach (object ob in temp.Children)
                    {
                        if (ob is CheckBox)
                        {
                            CheckBox c = (CheckBox)ob;
                            bool check = (bool)c.IsChecked;
                            if (check)
                            {
                                foreach (MultipleChoiceQuestion question in q)
                                {
                                    if (question.CorrectAnswers.Contains(c.Content))
                                        countRight++;
                                    else
                                        countWrong++;
                                }
                            }
                        }
                        percent = ((double)countRight / numberOfCorrect) * 100;
                        if (percent < 0)
                            percent = 0;

                        asmntResultGrid.Visibility = Visibility.Visible;
                        asmntResultLbl.Content = "You scored " + percent + " %";
                        resultString = percent.ToString();
                
                    }
                }
            }
            Database.SubmitMc(this, main);
        }
    }
    
    static class Extensions
    {
        static readonly Random random = new Random();
        public static void Shuffle<T>(this IList<T> list)
        {
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = random.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
    }
}
